package utility;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Build;
import android.view.Display;
import android.view.WindowManager;

public class ScreenUtility {
	@SuppressLint("NewApi")
	public static int getWidth(Context mContext){
	    int width=0;
	    WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
	    Display display = wm.getDefaultDisplay();
	    if(Build.VERSION.SDK_INT>12){                   
	        Point size = new Point();
	        display.getSize(size);
	        width = size.x;
	    }
	    else{
	        width = display.getWidth();  // deprecated
	    }
	    return width;
	}
	
	@SuppressLint("NewApi")
	public static int getHeight(Context mContext){
	    int height=0;
	    WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
	    Display display = wm.getDefaultDisplay();
	    if(Build.VERSION.SDK_INT>12){               
	        Point size = new Point();
	        display.getSize(size);
	        height = size.y;
	    }else{          
	        height = display.getHeight();  // deprecated
	    }
	    return height;      
	}
	
	@SuppressLint("NewApi")
	/*
	 * The method getScreenOrientation() return screen orientation either
	 * landscape or portrait. IF width < height , than orientation = portrait,
	 * ELSE landscape For backwards compatibility we use to methods to detect
	 * the orientation. The first method is for API versions prior to 13 or
	 * HONEYCOMB.
	 */
	public static int getScreenOrientation(Context context)
	{
		
		
		int currentapiVersion = android.os.Build.VERSION.SDK_INT;
	    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		//if API version less than 13 
		 Display getOrient = wm.getDefaultDisplay();
		    int orientation = Configuration.ORIENTATION_UNDEFINED;
			
		if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){
		    // Do something for API version less than HONEYCOMB
			 
			    if(getOrient.getWidth()==getOrient.getHeight()){
			        orientation = Configuration.ORIENTATION_SQUARE;
			    } else{ 
			        if(getOrient.getWidth() < getOrient.getHeight()){
			            orientation = Configuration.ORIENTATION_PORTRAIT;
			        }else { 
			             orientation = Configuration.ORIENTATION_LANDSCAPE;
			        }
			    }
		} 
		else{
			// Do something for API version greater or equal to HONEYCOMB
			
		    Point size = new Point();
		    wm.getDefaultDisplay().getSize(size);
		    int width = size.x;
		    int height = size.y;
		    
		    if (width < height)
		    {
		    	 orientation = Configuration.ORIENTATION_PORTRAIT;
		    }
		    else { 
	             orientation = Configuration.ORIENTATION_LANDSCAPE;
	        }
		}
		
		 return orientation;
		
		
	}//end getScreenOrientation()





}
